package aimsite

import (
	"banzhuan-sys/model"
	"banzhuan-sys/pkg/db"
	"banzhuan-sys/pkg/utils"
	"fmt"
	"github.com/gocolly/colly"
	"gorm.io/gorm"
	"time"
)

type Xmf struct {
	Title string
	Url   string
}

func (xmf *Xmf) Run() (bool, error) {

	var posts []model.Posts
	var all []model.Posts
	post := model.Posts{}
	var count int64
	db.My.Model(&model.Posts{}).Where("cat_id = ?", XMF_ID).Count(&count)

	c := colly.NewCollector(
		colly.UserAgent(utils.RandomUA()),
		//colly.MaxDepth(5),
	)

	c.Limit(&colly.LimitRule{
		DomainGlob: "*",
		Delay:      1 * time.Second,
	})

	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL)
	})

	c.OnHTML(".pc-wrap", func(e *colly.HTMLElement) {

		posts = make([]model.Posts, 0)

		e.ForEach(".item-wrap", func(i int, el *colly.HTMLElement) {

			node := el.DOM.Find(".position-name ").Children()
			post.Title = node.Text()
			post.Href, _ = node.Attr("href")
			post.Href = XMF + post.Href
			post.CatId = XMF_ID

			if count == 0 {
				posts = append(posts, post)
			} else {
				res := db.My.Where("href = ?", post.Href).First(&model.Posts{})
				if res.Error == gorm.ErrRecordNotFound {
					posts = append(posts, post)
				} else {
					return
				}
			}

		})

		if len(posts) == 0 {
			return
		}

		all = append(all, posts...)

	})

	c.OnHTML(".el-pagination", func(e *colly.HTMLElement) {

		nextUrl, res := e.DOM.Next().Attr("href")
		nextUrl = XMF + nextUrl
		if len(posts) == 0 {
			return
		}
		if res {
			e.Request.Visit(nextUrl)
		}

	})

	c.OnScraped(func(r *colly.Response) {

		fmt.Println("结束", r.Request.URL)
		page := r.Request.URL.Path
		if page == "/work/o1-p1" {
			for i, j := 0, len(all)-1; i < j; i, j = i+1, j-1 {
				all[i], all[j] = all[j], all[i]
			}
			//编码utf8mb4_general_ci
			db.My.Create(&all)
		}
	})

	c.Visit(XMF + "/work/o1-p1")

	return true, nil

}
